package tw.raybird.walker.officereader;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;


public class PDFReader implements Reader {

	private Logger logger;
	
	public PDFReader(){
		logger = Logger.getRootLogger();
	}
	
	@Override
	public String read(File file) {
		String content = "";
		PDDocument document = null;
		try {
			document = PDDocument.load(file);
			PDFTextStripper textStripper = new PDFTextStripper();
			content = textStripper.getText(document);
		} catch (Exception e) {
			logger.warn("pdf讀檔錯誤", e);
		}finally{
			try {
				if(document != null)
					document.close();
			} catch (IOException e) {
				logger.warn("", e);
			}
			document = null;
		}
		return content;
	}
}
